﻿using AgencyManagerSystem.Common;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.DataUpdater;
using AgencyManagerSystem.Model;
using AgencyManagerSystem.AsmType;
using System;
namespace AgencyManagerSystem.Core
{
    public class DepartmentUpdaterCreator : BaseUpdaterCreator<DepartmentModel>
    {
        public DepartmentUpdater CreateDepartmentUpdaterInstance()
        {
            DepartmentUpdater departmentUpdater = new DepartmentUpdater(new DepartmentFetcher());
            departmentUpdater.DataUpdating += FlagAndApproveFlagValid;
            departmentUpdater.DataUpdating += ParentCheck;
            departmentUpdater.DataUpdating += CodeCheck;
            departmentUpdater.DataDeleting += ChildExist;
            departmentUpdater.DataDeleting += UserExist;
            return departmentUpdater;
        }

        private void UserExist(BaseUpdater<DepartmentModel> sender, DataDeletingEventArgs<DepartmentModel> e)
        {
            if (e.Delete == null) return;
            var userFetcher = new UserFetcher();
            var users = userFetcher.ListByDepartment(e.Delete.AutoId);
            if ( users.Count>0)
            {
                e.Cancel = true;
                e.Message = String.Format("部门{0}已存在普通用户{1}等{2}笔记录", e.Delete.Code, users[0].Name, users.Count);
                return;
            }
        }
        private void ChildExist(BaseUpdater<DepartmentModel> sender, DataDeletingEventArgs<DepartmentModel> e)
        {
            if (e.Delete == null) return;
            var departmentFetcher = new DepartmentFetcher();
            var departments = departmentFetcher.ListByParent(e.Delete.AutoId);
            if (departments.Count > 0)
            {
                e.Cancel = true;
                e.Message = "该部门已存下级部门";
                e.Message = String.Format("部门{0}已存在下级部门{1}等{2}笔记录", e.Delete.Code, departments[0].Name, departments.Count);
                return;
            }
        }
        private void CodeCheck(BaseUpdater<DepartmentModel> sender, DataUpdatingEventArgs<DepartmentModel> e)
        {
            if (e.Insert == null) return;
            DepartmentFetcher departmentFetcher = new DepartmentFetcher();
            var dep = departmentFetcher.GetByCode(e.Insert.Code);
            if (dep !=null&&dep.AutoId!=e.Insert.AutoId)
            {
                e.Cancel = true;
                e.Message = String.Format("编号{0}已被部门{1}使用",dep.Code,dep.AutoId);
                return;
            }
        }
        private void ParentCheck(BaseUpdater<DepartmentModel> sender, DataUpdatingEventArgs<DepartmentModel> e)
        {
            if (e.Insert == null) return;
            DepartmentFetcher departmentFetcher = new DepartmentFetcher();
            if(e.Insert.Parent==0)
            {
                var root = departmentFetcher.GetRoot();
                if ( root != null)
                {
                    e.Cancel = true;
                    e.Message = String.Format( "已经存在一个上级部门{0}",root.AutoId);
                    return;
                }
            }
            else
            {
                var parent = departmentFetcher.Get(e.Insert.Parent);
                if (parent == null)
                {
                    e.Cancel = true;
                    e.Message = String.Format("上级部门{0}不存在",e.Insert.Parent);
                    return;
                }
            }
        }
    }
}
